package com.yq.cp.service.yw.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.yq.cp.config.MyException;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.GridSettlementUnitPrice;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.TotalSettlement;
import com.yq.cp.entity.data.dto.YwZbglJjbDto;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.yw.YwZbgl;
import com.yq.cp.entity.yw.YwZbglGlry;
import com.yq.cp.entity.yw.YwZbglJjb;
import com.yq.cp.entity.yw.YwZbglXq;
import com.yq.cp.entity.yw.vo.YwZbglJjbVo;
import com.yq.cp.mapper.permission.UserMapper;
import com.yq.cp.mapper.yw.YwZbglGlryMapper;
import com.yq.cp.mapper.yw.YwZbglJjbMapper;
import com.yq.cp.mapper.yw.YwZbglMapper;
import com.yq.cp.mapper.yw.YwZbglXqMapper;
import com.yq.cp.service.yw.YwZbglJjbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.PdfPageUtil;
import com.yq.cp.utils.UrlConvertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.List;

/**
 * <p>
 * 运维-值班管理-交接班 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
@Transactional
public class YwZbglJjbServiceImpl extends ServiceImpl<YwZbglJjbMapper, YwZbglJjb> implements YwZbglJjbService {

    @Resource
    YwZbglJjbMapper ywZbglJjbMapper;
    @Resource
    YwZbglGlryMapper ywZbglGlryMapper;
    @Resource
    YwZbglXqMapper ywZbglXqMapper;
    @Resource
    YwZbglMapper ywZbglMapper;
    @Resource
    UserMapper userMapper;

    @Resource
    UrlConvertUtil urlConvertUtil;
    @Resource
    YwZbglJjbService ywZbglJjbService;
    @Override
    public void add(YwZbglJjb ywZbglJjb) {
        long l = System.currentTimeMillis();
        ywZbglJjb.setId(l);
        //查询上一个值班的
        QueryWrapper<YwZbglJjb> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("state",1);
        queryWrapper2.eq("sszd_id",ywZbglJjb.getSszdId());
        queryWrapper2.orderByDesc("id");
        List<YwZbglJjb> ywZbglJjbs = ywZbglJjbMapper.selectList(queryWrapper2);
        if (ywZbglJjbs!=null && ywZbglJjbs.size()>0){
            YwZbglJjb ywZbglJjb1 = ywZbglJjbs.get(0);
            ywZbglJjb1.setState(4);
            ywZbglJjbMapper.updateById(ywZbglJjb1);

            ywZbglJjb.setJbsj(ywZbglJjb1.getJbsj());
            ywZbglJjb.setZbsj(ywZbglJjb1.getJbzbsj());
            ywZbglJjb.setKssj(ywZbglJjb1.getJbkssj());
            ywZbglJjb.setJssj(ywZbglJjb1.getJbjssj());

            //写入交班人员
            QueryWrapper<YwZbglGlry> queryWrapper5=new QueryWrapper<>();
            queryWrapper5.eq("gl_id",ywZbglJjb1.getId());
            queryWrapper5.eq("fl",3);
            List<YwZbglGlry> ywZbglGlries = ywZbglGlryMapper.selectList(queryWrapper5);
            if (ywZbglGlries==null || ywZbglGlries.size()==0){

            }else {
                for (int i = 0; i < ywZbglGlries.size(); i++) {
                    YwZbglGlry glry = ywZbglGlries.get(i);
                    glry.setId(null);
                    glry.setGlId(ywZbglJjb.getId());
                    glry.setFl(2);
                    ywZbglGlryMapper.insert(glry);

                }
            }

            ywZbglJjb.setDqbz(ywZbglJjb1.getJbbz());
        }
        //查询下一个接班时间段

        YwZbglXq xq = ywZbglXqMapper.selectById(ywZbglJjb.getXqId());
        if (xq==null){
            throw new MyException("参数错误:xqId");
        }
//        ywZbglJjb.setZbsj(xq.getZbsj());
//        ywZbglJjb.setKssj(xq.getKssj());
//        ywZbglJjb.setJssj(xq.getJssj());
        ywZbglJjb.setState(0);

        ywZbglJjb.setJbzbsj(xq.getZbsj());
        ywZbglJjb.setJbkssj(xq.getKssj());
        ywZbglJjb.setJbjssj(xq.getJssj());

        QueryWrapper<YwZbglGlry> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("gl_id",ywZbglJjb.getXqId());
        List<YwZbglGlry> ywZbglGlries = ywZbglGlryMapper.selectList(queryWrapper);
        if (ywZbglGlries==null || ywZbglGlries.size()==0){
            throw new MyException("参数错误:xqId");
        }
        for (int i = 0; i < ywZbglGlries.size(); i++) {
            YwZbglGlry ywZbglGlry = ywZbglGlries.get(i);
            ywZbglGlry.setId(null);
            ywZbglGlry.setGlId(ywZbglJjb.getId());
            ywZbglGlry.setFl(3);
            ywZbglGlryMapper.insert(ywZbglGlry);
            if (ywZbglGlry.getType()==1){
                User user = userMapper.selectById(ywZbglGlry.getUserId());
                ywZbglJjb.setJbbz(user.getNickname());
            }
        }
        ywZbglJjbMapper.insert(ywZbglJjb);
    }

    @Override
    public void sc(YwZbglJjb ywZbglJjb) {
        ywZbglJjbMapper.sc(ywZbglJjb.getId());
        ywZbglGlryMapper.sc(ywZbglJjb.getId());
    }

    @Override
    public void jb(YwZbglJjb ywZbglJjb) {
        Long userId = UserThreadLocal.get().getId();
        YwZbglJjb ywZbglJjb1 = ywZbglJjbMapper.selectById(ywZbglJjb.getId());
        if (ywZbglJjb1!=null){
            //查询是否是班长
            QueryWrapper<YwZbglGlry> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("gl_id",ywZbglJjb1.getXqId());
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("type",1);
            YwZbglGlry ywZbglGlry = ywZbglGlryMapper.selectOne(queryWrapper);
//            if (ywZbglGlry==null){
//                throw new MyException("没有权限操作");
//            }

            //查询之前已接班的
            QueryWrapper<YwZbglJjb> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("state",1);
            queryWrapper2.eq("sszd_id",ywZbglJjb1.getSszdId());
            queryWrapper2.orderByDesc("id");
            List<YwZbglJjb> ywZbglJjbs = ywZbglJjbMapper.selectList(queryWrapper2);
            if (ywZbglJjbs!=null && ywZbglJjbs.size()>0){
                YwZbglJjb ywZbglJjb9 = ywZbglJjbs.get(0);
                ywZbglJjb9.setState(4);
                ywZbglJjbMapper.updateById(ywZbglJjb9);
            }
            
            ywZbglJjb1.setState(1);
            ywZbglJjbMapper.updateById(ywZbglJjb1);



        }
    }

    @Override
    public IPage<YwZbglJjb> page(YwZbglJjbVo vo) {
        IPage<YwZbglJjb> ipage=new Page<>(vo.getPage(),vo.getSize());
        if (vo.getRq()!=null && !vo.getRq().equals("")){
            vo.setBeginTime(DateTimeUtil.toSimpleDateTime(vo.getRq()+" 00:00:00"));
            vo.setOverTime(DateTimeUtil.toSimpleDateTime(vo.getRq()+" 23:59:59"));
        }
        ywZbglJjbMapper.page(ipage, vo);

        return ipage;
    }

    @Override
    public List<YwZbglJjbDto> daochu(YwZbglJjbVo vo) {
        vo.setPage(1);
        vo.setSize(1000);
        IPage<YwZbglJjb> page = page(vo);
        List<YwZbglJjb> records = page.getRecords();
        List<YwZbglJjbDto> list=new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            YwZbglJjb ywZbglJjb = records.get(i);

            YwZbglJjbDto dto=new YwZbglJjbDto();
            dto.setId(i+1+"");
            dto.setSszdName(ywZbglJjb.getSszdName());
            dto.setDqbz(ywZbglJjb.getDqbz());
            dto.setJbbz(ywZbglJjb.getJbbz());
            String state="";
            switch (ywZbglJjb.getState()){
                case 0:
                    state="待接班";
                    break;
                case 1:
                    state="已接班";
                    break;
                case 2:
                    state="待交班";
                    break;
                case 4:
                    state="已交班";
                    break;
                case 99:
                    state="交接班异常";
                    break;
            }
            dto.setState(state);
            dto.setJbsj(DateTimeUtil.getStrDateTime3(ywZbglJjb.getJbsj()));
            dto.setCreateTime(DateTimeUtil.getStrDateTime3(ywZbglJjb.getCreateTime()));
            list.add(dto);
        }
        return list;
    }

    @Override
    public YwZbglJjb get(Long id) {
        YwZbglJjb ywZbglJjb = ywZbglJjbMapper.selectById(id);

        Map<String,Object> map=new HashMap<>();
        List<YwZbglGlry> all1 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 1,2);
        List<YwZbglGlry> all2 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 2,2);
        List<YwZbglGlry> all3 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 3,2);
        if (all1!=null && all1.size()>0){
            map.put("a1",all1.get(0));
        }else {
            map.put("a1",null);
        }
        if (all2!=null && all2.size()>0){
            map.put("a2",all2.get(0));
        }else {
            map.put("a2",null);
        }
        map.put("a3",all3);
        ywZbglJjb.setM1(map);

        map=new HashMap<>();
         all1 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 1,3);
         all2 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 2,3);
         all3 = ywZbglGlryMapper.all(ywZbglJjb.getId(), 3,3);
        if (all1!=null && all1.size()>0){
            map.put("a1",all1.get(0));
        }else {
            map.put("a1",null);
        }
        if (all2!=null && all2.size()>0){
            map.put("a2",all2.get(0));
        }else {
            map.put("a2",null);
        }
        map.put("a3",all3);

        ywZbglJjb.setM2(map);
        return ywZbglJjb;
    }

    @Override
    public Map<String, Object> cxxyczbxx(YwZbglJjb ywZbglJjb) {
        //查询上一个值班的
        QueryWrapper<YwZbglJjb> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("state",1);
        queryWrapper2.eq("sszd_id",ywZbglJjb.getSszdId());
        queryWrapper2.orderByDesc("id");
        List<YwZbglJjb> ywZbglJjbs = ywZbglJjbMapper.selectList(queryWrapper2);

        YwZbglXq xq=new YwZbglXq();
        if (ywZbglJjbs!=null && ywZbglJjbs.size()>0) {
            YwZbglJjb ywZbglJjb1 = ywZbglJjbs.get(0);

            Long xqId = ywZbglJjb1.getXqId();
            xq = ywZbglXqMapper.selectById(xqId);

            xq.setRq(DateTimeUtil.getStrDateTime3(xq.getSj()));
            xq.setRqsj(DateTimeUtil.getStrDateTime5(xq.getJssj()));
            xq.setZbglId(null);
            xq.setSszdId(ywZbglJjb.getSszdId());
//            xq.setXqId(ywZbglJjb1.getXqId());
            YwZbglXq xq1 = ywZbglXqMapper.selectById(ywZbglJjb1.getXqId());
            xq.setPx(xq1.getPx());
        }else{

            xq.setSszdId(ywZbglJjb.getSszdId());
            QueryWrapper<YwZbgl> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("nyz_id",ywZbglJjb.getSszdId());
            queryWrapper.eq("zj",DateTimeUtil.zhouji(DateTimeUtil.dqsj()));
            List<YwZbgl> ywZbgls = ywZbglMapper.selectList(queryWrapper);
            xq.setZbglId(ywZbgls.get(0).getId());

        }
        Map<String,Object> map=new HashMap<>();
        YwZbglXq cx = ywZbglXqMapper.cx1(xq);
        if (cx==null){
            cx = ywZbglXqMapper.cxdyg(xq);
        }
        if (cx==null){
            return map;
        }


        List<YwZbglGlry> all1 = ywZbglGlryMapper.all(cx.getId(), 1,1);
        List<YwZbglGlry> all2 = ywZbglGlryMapper.all(cx.getId(), 2,1);
        List<YwZbglGlry> all3 = ywZbglGlryMapper.all(cx.getId(), 3,1);
        if (all1!=null && all1.size()>0){
            map.put("a1",all1.get(0));
        }else {
            map.put("a1",null);
        }
        if (all2!=null && all2.size()>0){
            map.put("a2",all2.get(0));
        }else {
            map.put("a2",null);
        }
        map.put("a3",all3);
        map.put("kssj",cx.getKssj1());
        map.put("jssj",cx.getJssj1());
        map.put("zbsj",cx.getZbsj());
        map.put("jbsj",DateTimeUtil.getStrDateTime(DateTimeUtil.dqsj()));
        map.put("xqId",cx.getId());

        return map;
    }
    @Override
    public Map<String, Object> cxdqzbxx1(YwZbglJjb ywZbglJjb) {
        Map<String, Object> map=new HashMap<>();

        QueryWrapper<YwZbglJjb> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("sszd_id",ywZbglJjb.getId());
        queryWrapper.in("state",0,1);
        queryWrapper.orderByDesc("id");
        YwZbglJjb ywZbglJjb1 = ywZbglJjbMapper.selectOne(queryWrapper);


        if (ywZbglJjb1==null){
            map.put("sm","");
            map.put("a1","");
            map.put("a2","");
            map.put("a3",null);
            return map;
        }
        QueryWrapper<YwZbglGlry> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("gl_id",ywZbglJjb1.getId());
        if (ywZbglJjb1.getState()==0){
            queryWrapper1.eq("fl",2);
        }else {
            queryWrapper1.eq("fl",3);
        }
        List<YwZbglGlry> ywZbglGlries = ywZbglGlryMapper.selectList(queryWrapper1);
        YwZbglGlry list1=new YwZbglGlry();
        YwZbglGlry list2=new YwZbglGlry();
        List<YwZbglGlry> list3=new ArrayList<>();
        for (int i = 0; i < ywZbglGlries.size(); i++) {
            YwZbglGlry ywZbglGlry = ywZbglGlries.get(i);
            User user = userMapper.selectById(ywZbglGlry.getUserId());
            ywZbglGlry.setUserName(user.getNickname());
            ywZbglGlry.setUserImg(user.getHeadPortrait());
            if (ywZbglGlry.getType()==1){
                list1=ywZbglGlry;
            }else if (ywZbglGlry.getType()==2){
                list2=ywZbglGlry;
            }else{
                list3.add(ywZbglGlry);
            }

        }
        map.put("dbrz",ywZbglJjb1.getDbrz());
        map.put("a1",list1);
        map.put("a2",list2);
        map.put("a3",list3);
        return map;
    }

    @Override
    public Long cxdqzbxx(YwZbglJjb ywZbglJjb) {
        //查询上一个值班的
        QueryWrapper<YwZbglJjb> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.in("state",1,0);
        queryWrapper2.eq("sszd_id",ywZbglJjb.getSszdId());
        queryWrapper2.orderByDesc("id");
        List<YwZbglJjb> ywZbglJjbs = ywZbglJjbMapper.selectList(queryWrapper2);
        Long xqId=null;
        YwZbglXq xq=new YwZbglXq();
        if (ywZbglJjbs!=null && ywZbglJjbs.size()>0) {
            YwZbglJjb ywZbglJjb1 = ywZbglJjbs.get(0);

            xqId = ywZbglJjb1.getXqId();
            return xqId;
        }


        return xqId;
    }

    @Override
    public List<YwZbglGlry> dqzbry(Long nyzId) {
        QueryWrapper<YwZbglJjb> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("state",1,0);
        queryWrapper.eq("sszd_id",nyzId);
        queryWrapper.orderByDesc("id");
        YwZbglJjb ywZbglJjb = ywZbglJjbMapper.selectOne(queryWrapper);
        if (ywZbglJjb==null){
            return null;
        }
        QueryWrapper<YwZbglGlry> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("gl_id",ywZbglJjb.getId());
        if (ywZbglJjb.getState()==0){
            queryWrapper1.eq("fl",2);
        }else {
            queryWrapper1.eq("fl",3);
        }

        List<YwZbglGlry> ywZbglGlries = ywZbglGlryMapper.selectList(queryWrapper1);
        return ywZbglGlries;
    }

    @Override
    public void csh(Long sszdId) {
        ywZbglJjbMapper.xgzt(sszdId);
        YwZbglJjb ywZbglJjb=new YwZbglJjb();
        ywZbglJjb.setSszdId(sszdId);
        Map<String, Object> cxxyczbxx = cxxyczbxx(ywZbglJjb);

        YwZbglJjb jjb=new YwZbglJjb();
        jjb.setSszdId(sszdId);
        jjb.setXqId((Long) cxxyczbxx.get("xqId"));
        jjb.setJbsj(DateTimeUtil.dqsj());
        add(jjb);

    }

    @Override
    public void getDaochu(YwZbglJjb ywZbglJjb, HttpServletResponse response) {


        // 定义全局的字体静态变量
        //定义全局字体静态变量
        Font titlefont;
        Font headfont = null;
        Font headfont1 = null;
        Font keyfont = null;
        Font textfont = null;
        Font textfont1 = null;
        Font content = null;
        Font space = null;
        Font space1 = null;
        Font space2 = null;
        Font space3 = null;
        // 最大宽度
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(font, 16, Font.BOLD);
            //四号
            headfont = new Font(font, 14, Font.BOLD);
            headfont1 = new Font(font, 14, Font.NORMAL);
            //三号
            content = new Font(font, 16, Font.NORMAL);
            //小四
            textfont = new Font(font, 11, Font.BOLD);
            textfont1 = new Font(font, 11, Font.NORMAL);
            space = new Font(font, 2, Font.NORMAL);
            space1 = new Font(font, 10, Font.NORMAL);
            space2 = new Font(font, 30, Font.NORMAL);
            space3 = new Font(font, 20, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseFont bf;
        Font font = null;
        try {
            //创建字体
            bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            //使用字体并给出颜色
            font = new Font(bf,20,Font.BOLD, BaseColor.BLACK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document document = new Document(new RectangleReadOnly(842F, 595F));
        //设置页边距  60:左边距，60：右边距，72：上边距，72：下边距
        document.setMargins(60, 60, 72, 72);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            PdfPCell cell = null;
            //添加页码
            writer.setPageEvent(new PdfPageUtil());
            //打开生成的pdf文件
            document.open();



            //交接班信息
            float[] widthes = {30f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            PdfPTable tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("交接班信息",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            document.add(tables);


            widthes = new float[]{20f, 20f, 20f, 20f, 20f, 20f, 20f, 20f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("值班负责人:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            String aaa="";
            if (ywZbglJjb.getM1()!=null){
                Object a1 = ywZbglJjb.getM1().get("a1");
                if (a1!=null){
                    YwZbglGlry ywZbglGlry=(YwZbglGlry) a1;
                    aaa = ywZbglGlry.getUserName();
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("安全员:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            if (ywZbglJjb.getM1()!=null){
                Object a1 = ywZbglJjb.getM1().get("a2");
                if (a1!=null){
                    YwZbglGlry ywZbglGlry=(YwZbglGlry) a1;
                    aaa = ywZbglGlry.getUserName();
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("值班人员:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            if (ywZbglJjb.getM1()!=null){
                List<YwZbglGlry> a1 = (List<YwZbglGlry>)ywZbglJjb.getM1().get("a3");
                aaa="";
                if (a1!=null){
                    List<YwZbglGlry> ywZbglGlries = a1;
                    for (int i = 0; i < ywZbglGlries.size(); i++) {
                        YwZbglGlry ywZbglGlry = ywZbglGlries.get(i);
                        if (i==0){
                            aaa=ywZbglGlry.getUserName();
                        }else {
                            aaa=","+ywZbglGlry.getUserName();
                        }

                    }
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("值班时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);

            if (ywZbglJjb.getZbsj()!=null){
                aaa=ywZbglJjb.getZbsj()+"  "+DateTimeUtil.getStrDateTime5(ywZbglJjb.getKssj())+"  "+DateTimeUtil.getStrDateTime5(ywZbglJjb.getJssj());
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph(DateTimeUtil.getStrDateTime3(ywZbglJjb.getJbsj()),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);

            widthes= new float[]{10f,90f};;
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("交班时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);



            if (ywZbglJjb.getZbsj()!=null){
                aaa=DateTimeUtil.getStrDateTime(ywZbglJjb.getJbsj());
            }else {
                aaa="";
            }

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);
            document.add(tables);


            //接班信息
            widthes = new float[]{30f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("接班信息",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            document.add(tables);


            widthes = new float[]{20f, 20f, 20f, 20f, 20f, 20f, 20f, 20f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("值班负责人:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            if (ywZbglJjb.getM2()!=null){
                Object a1 = ywZbglJjb.getM1().get("a1");
                if (a1!=null){
                    YwZbglGlry ywZbglGlry=(YwZbglGlry) a1;
                    aaa = ywZbglGlry.getUserName();
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("安全员:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            if (ywZbglJjb.getM2()!=null){
                Object a1 = ywZbglJjb.getM1().get("a2");
                if (a1!=null){
                    YwZbglGlry ywZbglGlry=(YwZbglGlry) a1;
                    aaa = ywZbglGlry.getUserName();
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);



            if (ywZbglJjb.getM2()!=null){
                List<YwZbglGlry> a1 = (List<YwZbglGlry>)ywZbglJjb.getM2().get("a3");
                aaa="";
                if (a1!=null){
                    List<YwZbglGlry> ywZbglGlries = a1;
                    for (int i = 0; i < ywZbglGlries.size(); i++) {
                        YwZbglGlry ywZbglGlry = ywZbglGlries.get(i);
                        if (i==0){
                            aaa=ywZbglGlry.getUserName();
                        }else {
                            aaa=","+ywZbglGlry.getUserName();
                        }

                    }
                }else {
                    aaa="";
                }
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph("值班人员:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("值班时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            if (ywZbglJjb.getZbsj()!=null){
                aaa=ywZbglJjb.getJbzbsj()+"  "+DateTimeUtil.getStrDateTime5(ywZbglJjb.getJbkssj())+"  "+DateTimeUtil.getStrDateTime5(ywZbglJjb.getJbjssj());
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);

            widthes= new float[]{10f,90f};;
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("交班时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            if (ywZbglJjb.getZbsj()!=null){
                aaa=DateTimeUtil.getStrDateTime(ywZbglJjb.getJbsj());
            }else {
                aaa="";
            }
            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);
            document.add(tables);


            //接班说明
            widthes = new float[]{30f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("交班说明",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            document.add(tables);


            //当班日志
            widthes = new float[]{10f,90f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("当班日志:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(ywZbglJjb.getDbrz(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);



            //日志图片
            widthes = new float[]{30f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("日志图片:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);
            document.add(tables);

//            Image image = Image.getInstance("E:\\zh\\yunzaikafei\\file\\2023\\05\\29\\11.png"); // 替换为你的图片路径
//            // 设置图片大小
//            float imageWidth = 50f; // 设置图片的宽度
//            float imageHeight = image.getScaledHeight() * (imageWidth / image.getScaledWidth()); // 根据宽度计算高度
//            image.setAlignment(Image.ALIGN_LEFT); // 设置图片居中
//            image.scaleAbsolute(imageWidth, imageHeight); // 设置图片的绝对大小
//            document.add(image);
            PdfPTable table =null;
            List<String> zh = urlConvertUtil.zh(ywZbglJjb.getRztp());
            if (zh.size()>0){
                table = new PdfPTable(zh.size()); // 3 columns for 3 images in a row
                table.setWidthPercentage(100); // Set the width of the table to 100% of the page width

                //故障图片上传
                for (int i = 0; i < zh.size(); i++) {
                    String s = zh.get(i);
                    Image image = Image.getInstance(s); // 替换为你的图片路径
                    // 设置图片大小
                    float imageWidth = 10f; // 设置图片的宽度
                    float imageHeight = image.getScaledHeight() * (imageWidth / image.getScaledWidth()); // 根据宽度计算高度
                    image.setAlignment(Image.ALIGN_LEFT); // 设置图片居中
                    image.scaleAbsolute(imageWidth, imageHeight); // 设置图片的绝对大小
                    image.setIndentationLeft(20f);

                    cell = new PdfPCell();
                    cell.setImage(image);
                    table.addCell(cell);
                }
                document.add(table);
            }



            //未处理故障
            widthes = new float[]{10f,90f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("未处理故障:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(ywZbglJjb.getWclgzsm(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);
            document.add(tables);


            //故障图片上传
            widthes = new float[]{30f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("故障图片:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);

            zh = urlConvertUtil.zh(ywZbglJjb.getGztp());
            if (zh.size()>0){
                table = new PdfPTable(zh.size()); // 3 columns for 3 images in a row
                table.setWidthPercentage(100); // Set the width of the table to 100% of the page width

                //故障图片上传
                for (int i = 0; i < zh.size(); i++) {
                    String s = zh.get(i);
                    Image image = Image.getInstance(s); // 替换为你的图片路径
                    // 设置图片大小
                    float imageWidth = 10f; // 设置图片的宽度
                    float imageHeight = image.getScaledHeight() * (imageWidth / image.getScaledWidth()); // 根据宽度计算高度
                    image.setAlignment(Image.ALIGN_LEFT); // 设置图片居中
                    image.scaleAbsolute(imageWidth, imageHeight); // 设置图片的绝对大小
                    image.setIndentationLeft(20f);

                    cell = new PdfPCell();
                    cell.setImage(image);
                    table.addCell(cell);
                }
                document.add(table);
            }




            document.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
